from .celery import app
from home.models import Banner
from django.conf import settings
from home.serializer import BannerSerializer
from django.core.cache import cache


@app.task
def add_banner():
    from home.models import Banner
    Banner.objects.create(title='测试', image='/1.png', link='/banner', info='xxx', orders=99)
    return 'banner增加成功'

@app.task
def update_banner():
    # 查询数据库，拿到所有轮播图，序列化后，放到缓存中
    banner_list = Banner.objects.all().filter(is_delete=False, is_show=True).order_by('orders')[:settings.BANNER_COUNT]
    ser = BannerSerializer(instance=banner_list, many=True)
    # 如果在视图类，做序列化，因为视图类有request对象，
    # 所以向图像这种，会自动加前面的地址
    for item in ser.data:
        # print(ser.data)
        print(1111)
        item['image'] = settings.BACKEND_URL + item['image']
    cache.set('home_banner_list', ser.data)
    return True
